package com.xcity.game.battle.res;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.common.GameStaticConfig;

import naga.x.App;
import naga.x.game.time.TimeUpdater;

public class ResStageTypeInfo {
	protected int lastFightDay;//最近一次打的日子
	List<Integer> type2dayFightCount;//今日已打的次数
	List<Long> type2lastFightTime;//上次战斗时间 用于记CD
	
	public ResStageTypeInfo(){
	}
	public ResStageTypeInfo(int size){
		type2dayFightCount = new ArrayList<Integer>(size);
		type2lastFightTime = new ArrayList<Long>(size);
		for(int i = 0; i < size; i++){
			type2dayFightCount.add(0);
			type2lastFightTime.add(0L);
		}
	}

	//---serialize start
	public int getLastFightDay() {
		return lastFightDay;
	}
	public void setLastFightDay(int lastFightDay) {
		this.lastFightDay = lastFightDay;
	}
	public List<Integer> getType2dayFightCount() {
		return type2dayFightCount;
	}
	public void setType2dayFightCount(List<Integer> type2dayFightCount) {
		this.type2dayFightCount = type2dayFightCount;
	}
	public List<Long> getType2lastFightTime() {
		return type2lastFightTime;
	}
	public void setType2lastFightTime(List<Long> type2lastFightTime) {
		this.type2lastFightTime = type2lastFightTime;
	}
	//---serialize end

	@JSONField(serialize = false)
	public int getDayCountByType(int type){
		int today = TimeUpdater.getInstance().today();
		if(lastFightDay != today){
			for(int i = 0; i < type2dayFightCount.size(); i++){
				type2dayFightCount.set(i, 0);
			}
			lastFightDay = today;
		}
		return type2dayFightCount.get(type);
	}

	@JSONField(serialize = false)
	public int getLeftSecondByType(int type){
		long time = type2lastFightTime.get(type);
		long now = TimeUpdater.getInstance().now();
		
		if(now >= time + GameStaticConfig.resStageCDMin * 60 * 1000L){
			return 0;
		}else if(now < time){
			return 0;
		}else{
			return (int) ((time + GameStaticConfig.resStageCDMin * 60 * 1000L - now) / 1000);
		}
	}
	
	@JSONField(serialize = false)
	public void setLastFightTime(int type, long time){
		type2lastFightTime.set(type, time);
	}
	
	public boolean tryAddDayCount(int type){
		int today = TimeUpdater.getInstance().today();
		if(lastFightDay != today){
			for(int i = 0; i < type2dayFightCount.size(); i++){
				type2dayFightCount.set(i, 0);
			}
			lastFightDay = today;
		}
		int dayCount = getDayCountByType(type);
		if(dayCount >= App.getApp().getServiceManager().get(ResStageService.class).getTypeTempList().get(type).getMaxCount()){
			return false;
		}
		dayCount++;
		type2dayFightCount.set(type, dayCount);
		return true;
	}
	
	@Deprecated
	public void resetCount(){
		lastFightDay = 0;

		for(int i = 0; i < type2lastFightTime.size(); i++){
			type2lastFightTime.set(i, 0L);
		}
	}
}
